package com.rogo.layouts
{
	import mx.core.IVisualElement;
	
	import spark.components.supportClasses.GroupBase;
	import spark.layouts.supportClasses.LayoutBase;
	
	
	/**
	 * 
	 * 	Grid 方格布局
	 * 
	 * @author coco
	 */	
	public class AutoGridLayout extends LayoutBase
	{
		public function AutoGridLayout()
		{
			super();
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Properties
		//
		//--------------------------------------------------------------------------
		
		private var columnWidth:Number = 0;
		private var rowHeight:Number = 0;
		private var columnNumber:int = 0;
		private var rowNumber:int = 0;
		
		private var _gap:Number = 6;

		/**
		 * 	间隙值
		 * 
		 * 	@return 
		 */		
		public function get gap():Number
		{
			return _gap;
		}

		public function set gap(value:Number):void
		{
			_gap = value;
			
			if(target)
				target.invalidateDisplayList();
		}
		
		
		private var _columnCount:int = 4;

		/**
		 *　最大列数 
		 */
		public function get columnCount():int
		{
			return _columnCount;
		}

		/**
		 * @private
		 */
		public function set columnCount(value:int):void
		{
			_columnCount = value;
		}
		
		
		
		override public function set target(value:GroupBase):void
		{
			super.target = value;
		}

		
		//--------------------------------------------------------------------------
		//
		//  Function
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 
		 * 	更新视图
		 * 
		 * @param width
		 * @param height
		 * 
		 */		
		override public function updateDisplayList(width:Number, height:Number):void
		{
			var targetGroup:GroupBase = target;
			var element:IVisualElement;
			var sx:Number = 0;
			var sy:Number = 0;
			
			calculationColumnCount();
			calculationProperties(width, height);
			
			for(var i:int = 0; i < targetGroup.numElements; i++)
			{
				element = targetGroup.getElementAt(i);
				
				if(!element || !element.includeInLayout)
					continue;
				
				element.setLayoutBoundsSize(columnWidth, rowHeight);
				element.depth = 0;
				element.setLayoutBoundsPosition(sx, sy);
				
				sx += gap + columnWidth;
				if(sx >= width)
				{
					sx = 0;
					sy += gap + rowHeight;
				}
			}
		}
		
		
		public function getProperties(index:int):Object
		{
			var o:Object = new Object();
			o.width = columnWidth;
			o.height = rowHeight;
			
			var sx:Number = 0, sy:Number = 0;
			
			for(var i:int = 0; i < index; i++)
			{
				sx += gap + columnWidth;
				if(sx >= target.width)
				{
					sx = 0;
					sy += gap + rowHeight;
				}
			}
			
			o.x = sx;
			o.y = sy;
			
			return o;
		}
		
		
		/**
		 * 
		 * 	计算容器中存放布局元素的列数　行数　列宽　行宽
		 * 
		 * @param w	容器宽
		 * @param h	容器高
		 */		
		private function calculationProperties(w:Number, h:Number):void
		{
			var numLayoutElements:int = getNumElementsInLayout();
			columnNumber = Math.min(columnCount, numLayoutElements);
			rowNumber = Math.ceil(numLayoutElements / columnCount);
			columnWidth = (w - (columnNumber - 1) * gap) / columnNumber;
			rowHeight = (h - (rowNumber - 1) * gap) / rowNumber;
		}
		
		/**
		 *
		 * 	获取在布局中元素的个数
		 *  
		 * @return 
		 * 
		 */		
		private function getNumElementsInLayout():int
		{
			var nums:int = 0, element:IVisualElement;
			var targetGroup:GroupBase = target;
			var numElement:int = targetGroup.numElements;
			for(var i:int = 0; i < numElement; i++)
			{
				element = targetGroup.getElementAt(i);
				
				if(element.includeInLayout)
					nums++;
			}
			
			return nums;
		}
		
		/**
		 *	计算最优列数
		 *  
		 * @return
		 */		
		private function calculationColumnCount():void
		{
			if (!target) return;
			
			var nums:int = target.numElements;
			
			for (var i:int = 2; i < 4; i++)
			{
				if (nums == Math.pow(i, 2)) 
				{
					columnCount = i;
					return;  
				}
			}
			
			if (nums <=4)
			{
				columnCount = Math.min(nums, 4);
				return;
			}
			
			columnCount = Math.ceil(nums / 2) <= 4 ? Math.ceil(nums / 2) : 4;
		}
		
		
		//--------------------------------------------------------------------------
		//
		//  Effect
		//
		//--------------------------------------------------------------------------
	}
}